package com.yls.except.controller;

import com.yls.except.service.BankService;
import com.yls.except.util.GeneralResult;
import com.yls.except.util.IpAddressUitls;
import com.yls.except.util.RegularHandle.RegularJudge;
import com.yls.except.util.StringIsempty;
import com.yls.except.util.exception.ExceptionHandle;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 银行卡绑定controller
 * Created by xs on 2016/8/3.
 */
@Controller
@RequestMapping(value = "bankcontroller")
public class BankBindingController extends ExceptionHandle {
    @Resource(name = "bankService")
    private BankService bankService;

    /**
     * 添加银行卡绑定
     * @param req
     * @param resp
     */
    @RequestMapping(value = "bindingbank.action")
    public void bindingbank(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String type = req.getAttribute("type") + "";
        String name = req.getParameter("name");
        String bankname = req.getParameter("bankname");
        String bankaddress = req.getParameter("bankaddress");
        String card = req.getParameter("card");
        String phone = req.getParameter("phone");
        String reqip = IpAddressUitls.getIpAddr(req);
        if(StringIsempty.isallempty(uid) || StringIsempty.isallempty(type))
            GeneralResult.general(resp, list, 10001, 0);
        else if(StringIsempty.isallempty(name) || StringIsempty.isallempty(bankname) || StringIsempty.isallempty(bankaddress) || StringIsempty.isallempty(card) || StringIsempty.isallempty(phone))
            GeneralResult.general(resp, list, 20001, 0);
        else if(!RegularJudge.isphone(phone))
            GeneralResult.general(resp, list, 20027, 0);
        else {
            int tag = bankService.insertBindingBank(uid, name, bankname, bankaddress, card, phone, reqip);
            GeneralResult.general(resp, list, tag, 1);
        }
    }

    /**
     * 获取绑定银行卡信息
     * @param req
     * @param resp
     */
    @RequestMapping(value = "getbandingbanklist.action")
    public void getBandingBankList(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String type = req.getAttribute("type") + "";
        if(StringIsempty.isallempty(uid) || StringIsempty.isallempty(type))
            GeneralResult.general(resp, list, 10001, 0);
        else {
            list = bankService.selectBandingBankInfoByUid(uid);
            GeneralResult.resultList(list, resp);
        }
    }

    /**
     *  获取绑定银行卡详情
     * @param req
     * @param resp
     */
    @RequestMapping(value = "bindingbankinfo.action")
    public void bindingBankInfo(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String type = req.getAttribute("type") + "";
        String bandingnum = req.getParameter("bandingnum");
        if(StringIsempty.isallempty(uid) || StringIsempty.isallempty(type))
            GeneralResult.general(resp, list, 10001, 0);
        else if(StringIsempty.isallempty(bandingnum))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            Map<String, Object> bankinfo = bankService.selectBandingBankInfoById(bandingnum, uid);
            GeneralResult.generalMap(resp, bankinfo);
        }
    }

    /**
     * 修改银行卡绑定信息
     * @param req
     * @param resp
     */
    @RequestMapping(value = "editbindingbankinfo.action")
    public void editBindingBankInfo(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String type = req.getAttribute("type") + "";
        String bandingnum = req.getParameter("bandingnum");
        String name = req.getParameter("name");
        String bankname = req.getParameter("bankname");
        String bankaddress = req.getParameter("bankaddress");
        String card = req.getParameter("card");
        String phone = req.getParameter("phone");
        if(StringIsempty.isallempty(uid) || StringIsempty.isallempty(type))
            GeneralResult.general(resp, list, 10001, 0);
        else if(StringIsempty.isallempty(name) || StringIsempty.isallempty(bankname) || StringIsempty.isallempty(bankaddress) || StringIsempty.isallempty(card) || StringIsempty.isallempty(phone) || StringIsempty.isallempty(bandingnum))
            GeneralResult.general(resp, list, 20001, 0);
        else if(!RegularJudge.isphone(phone))
            GeneralResult.general(resp, list, 20027, 0);
        else {
            int tag = bankService.updateBindingBank(uid, name, bankname, bankaddress, card, phone, bandingnum);
            GeneralResult.general(resp, list, tag, 1);
        }
    }

    /**
     * 删除银行卡绑定信息
     * @param req
     * @param resp
     */
    @RequestMapping(value = "delbindingbankinfo.action")
    public void delBindingBankInfo(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String type = req.getAttribute("type") + "";
        String bandingnum = req.getParameter("bandingnum");
        if(StringIsempty.isallempty(uid) || StringIsempty.isallempty(type))
            GeneralResult.general(resp, list, 10001, 0);
        else if(StringIsempty.isallempty(bandingnum))
            GeneralResult.general(resp, list, 20001, 0);
        else {
            int tag = bankService.deleteBindingInfo(uid, bandingnum);
            GeneralResult.general(resp, list, tag, 1);
        }
    }

    /**
     * 提现申请
     * @param req
     * @param resp
     */
    @RequestMapping(value = "withdrawapply.action")
    public void withdrawApply(HttpServletRequest req, HttpServletResponse resp) {
        List<Object> list = new ArrayList<Object>();
        String uid = req.getAttribute("uid") + "";
        String type = req.getAttribute("type") + "";
        String bindingnum = req.getParameter("bindingnum");
        String withdrawmoney = req.getParameter("withdrawmoney");
        String reqip = IpAddressUitls.getIpAddr(req);
        if(StringIsempty.isallempty(uid) || StringIsempty.isallempty(type))
            GeneralResult.general(resp, list, 10001, 0);
        else if(!"1".equals(type) && !"2".equals(type))
            GeneralResult.general(resp, list, 20001, 0);
        else if(!RegularJudge.isnumberreg(withdrawmoney))
            GeneralResult.general(resp, list, 20064, 0);
        else {
            int tag = bankService.insertWithDrawApply(uid, bindingnum, withdrawmoney, reqip);
            GeneralResult.general(resp, list, tag, 1);
        }
    }

    public  static String getBankNum(String banknum) {
        if(banknum.length() <= 3)
            return banknum;
        else {
            String str = banknum.substring(0, banknum.length() - 3);
            String returnstr = "";
            for (int i = 0; i < str.length() / 4; i ++) {
                if(returnstr == null || "".equals(returnstr))
                    returnstr = "****";
                else
                    returnstr += " ****";
            }
            if(str.length() % 4 == 0)
                return returnstr + " " + banknum.substring(banknum.length() - 3, banknum.length());
            else {
                String surplus = "";
                for (int i = 0; i < str.length() - returnstr.replaceAll(" ", "").length(); i ++) {
                    if(i == 0) {
                        if(returnstr == null || "".equals(returnstr))
                            surplus += "*";
                        else
                            surplus += " *";
                    } else
                        surplus += "*";
                }
                return returnstr + surplus + " " + banknum.substring(banknum.length() - 3, banknum.length());
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(getBankNum("123"));
        System.out.println(getBankNum("23"));
        System.out.println(getBankNum("1234"));
        System.out.println(getBankNum("123412341234112"));
        System.out.println(getBankNum("1234123412341123"));
        System.out.println(getBankNum("12341234123412123"));
        System.out.println(getBankNum("123412341234123123"));
        System.out.println(getBankNum("1234123412341234123"));
    }
}